<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="礼券管理">
<style>
    .button-large {
        font-size: 16px;
    }
    hr {
        border-color: #009688;
    }
    .control-group {
        width: 1000px;
    }
    .webuploader-pick {
        background: none;
        padding: 0;
    }
</style>
<div class="xm-offline">
    <div class="row">
        <div class="panel">
            <div class="panel-header">
                <a href="${basepath}/rest/manage/voucher/toList">返回上一级</a>
            </div>
            <div class="panel-body">
            <#--form表单提交，相当于data-->
                <form id="addForm" class="form-horizontal" action="${basepath}/rest/manage/voucher/updateJson"
                      method="post">
                    <input type="hidden" name="id" value="${e.id!}">
                    <div class="control-group">
                        <label class="control-label">
                            <s>*</s>
                            礼券类型：
                        </label>
                        <div class="controls">
                            <div id="s1"><input type="hidden" id="hide" value="${e.type!}" name="type"></div>
                        </div>
                    </div>
                    <div class="control-group name">
                        <label class="control-label">
                            <s>*</s>
                            礼券名称：
                        </label>
                        <div class="controls">
                            <input type="text" class="control-text span-width inp span5"
                                   name="name" placeholder="请输入礼券名称" data-rules="{required:true}" value="${e.name!}">
                        </div>
                    </div>
                    <div class=" control-group start">
                        <label class="control-label">
                            <s>*</s>
                            开始日期：
                        </label>
                        <div class="time-controls controls">
                            <input name="startDate" class=" inp calendar bui-form-field-date bui-form-field span5"
                                   type="text" aria-disabled="false" aria-pressed="false" value="${e.startDate!}">
                        </div>
                    </div>
                    <div class=" control-group end">
                        <label class="control-label">
                            <s>*</s>
                            结束日期：
                        </label>
                        <div class="time-controls controls">
                            <input name="endDate" class=" inp calendar bui-form-field-date bui-form-field span5" type="text"
                                   aria-disabled="false" aria-pressed="false" value="${e.endDate!}">
                        </div>
                    </div>

                    <div class=" control-group number">
                        <label class="control-label">
                            <s>*</s>
                            礼券面值：
                        </label>
                        <div class="controls">
                            <input type="number" class="inp control-text span-width span5" value="${e.money!}"
                                   name="money" placeholder="请填写礼券面值" >
                        </div>
                    </div>
                    <div class=" control-group limit">
                        <label class="control-label">
                            <s>*</s>
                            满减限制：
                        </label>
                        <div class="controls">
                            <input type="text" class="inp control-text span-width span5"  value="${e.limitMoney!}"
                                   name="limitMoney" placeholder="请填写满减限制" data-rules="{required : true,number:true}">
                        </div>
                    </div>
                    <div class=" control-group">
                        <label class="control-label"> <s>*</s>门槛介绍：</label>
                        <div class="controls control-row-auto mar0 ">
                                <textarea data-rules="{required:true}" class="span5 control-row4" value="${e.introduce!}"   name="introduce" placeholder="请填写门槛介绍">
                                </textarea>
                        </div>
                    </div>
                    <h3 class="offset2">礼券图片
                        <a class="button button-primary pull-right" id="imagesBtn" style="height:20px">上传图片</a></h3>
                    <hr>
                    <input type="hidden" class="control-text span-width " name="picture" id="picture"
                           data-rules="{required:true}" data-messages="{required:'图片不能为空'}" value="${e.picture!}">

                    <div id="imagesGrid" class="xm-grid">

                    </div>
                    <div class="centered">
                        <button type="submit" class="button  button-large  button-success">确定</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    var Store = BUI.Data.Store;
    var Data = BUI.Data;
    var Grid = BUI.Grid;
    var Calendar = BUI.Calendar;
    var datePicker = new Calendar.DatePicker({
        trigger: '.calendar-time',
        showTime: true,
        autoRender: true
    });
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            if (data.success == true) {
                window.location.href = "${basepath}/rest/manage/voucher/toList"
            } else {
                BUI.Message.Alert(data.message, 'error');
                return;
            }
        }
    }).render();
    if('${e.type!}'=="discount"){
        $('.limit').find('input').val(0).end().hide()
    }
    BUI.use('bui/select', function (Select) {
        var items = [
                    {text: '代金券', value: 'cash'},
                    {text: '优惠券', value: 'discount'}
                ],
                select = new Select.Select({
                    render: '#s1',
                    valueField: '#hide',
                    items: items
                });
        select.render();
        select.on('change', function (ev) {
            $('.control-group').find('.inp').val("").end().show();
            if(ev.item.value=="discount"){
                $('.limit').find('input').val(0).end().hide()
            }
        });
    });
    /*--------------------------活动图片列表  begin--------------------*/
    /**
     * 图片信息
     * @type {*[]}
     */
    var imagesColumns = [
        {title: '序号',  elCls: 'center', width: '30%',renderer:function (value,data,index) {
                return (index+1);
            }},
        {
            title: '图片', dataIndex: 'url',elCls: 'center', width: '30%', renderer: function (data) {
                return '<img src="${basepath}/' + data + '" width="100px" height="100px"/>'
            }
        },
        {
            title: '编辑', dataIndex: 'title', elCls: 'center', width: '40%', renderer: function (data, obj, index) {
                return '<a href="javascript:delImages(' + index + ')">删除</a>';
            }
        }
    ];
    /**
     * 删除缓存图片信息
     * @param index
     */
    function delImages(index) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            var record = imagesStore.findByIndex(index);
            imagesStore.remove(record);
            var imgStr = imagesStore.getResult();
            var imgArr = new Array();
            for (var i = 0; i < imgStr.length; i++) {
                imgArr[i] = imgStr[i].url
            }
            $("#picture").val(imgArr[0]);
        }, 'question');
    }

    var imagesStore = new Store({});
    var imagesGrid = new Grid.Grid({
        render: '#imagesGrid',
        width: '100%',//如果表格使用百分比，这个属性一定要设置
        columns: imagesColumns,
        idField: 'title',
        store: imagesStore
    }).render();
    /*------------------------图片上传插件配置  begin-----------------------*/
    var uploader = WebUploader.create({
        auto: true,
        swf: '${staticpath}/ueditor/third-party/webuploader/Uploader.swf',
        server: '${basepath}/rest/manage/ued/config?action=uploadimage',
        pick: '#imagesBtn',//绑定事件
        resize: false,
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png,'
        }
    });

    //上传成功后
    uploader.on('uploadSuccess', function (file, response) {
        uploader.removeFile(file);//删除缓存
        imagesStore.add(response);//给store赋值
        var imgStr = imagesStore.getResult();
        //将数据源store转化成字符串赋值给input
        var imgArr = new Array();               //图片保存到数组中
        for (var i = 0; i < imgStr.length; i++) {
            imgArr[i] = imgStr[i].url;
        }
        $("#picture").val(imgArr[0]);
    });
    /*------------------------图片上传插件配置  end-----------------------*/
    /**
     * 初始化图片列表数据源
     * */
    (function () {
        var imgArr = $("#picture").val().split(',');
        for (var i = 0; i < imgArr.length; i++) {
            var obj = new Object();
            obj.url = imgArr[i];
            obj.state = "SUCCESS";
            imagesStore.add(obj);
        }
    })()
</script>

</@page.pageBase>